<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2018/8/28
 * Time: 17:30
 */
namespace app\index\controller;

use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use app\index\model\Borrow;
use think\Request;

class BorrowsShow extends Common{
    protected $request;
    protected $borrow_model;
    protected $a_model;
    protected $ac_model;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request = $request;
        $this->borrow_model = new Borrow();
        $this->a_model = new Archives();
        $this->ac_model = new ArchivesCategory();
    }

    //借阅给外部显示
    public function index(){
        header("Content-type: text/html; charset=utf-8");
        $id = $this->request->param('id');
        if(empty($id)){
            echo '<div style="color: #888888;font-size: 32px;text-align: center;margin-top: 300px;">未找到借阅文件</div>';
        }
        $data = $this->borrow_model->getDataByCondition(array('bor_id'=>$id,'bor_status'=>2));
        if(empty($data)){
            echo '<div style="color: #888888;font-size: 32px;text-align: center;margin-top: 300px;">未找到借阅文件</div>';
        }
        $a_data = $this->a_model->getDataByCondition(array('arc_id'=>$data['arc_id']));
        $ac_data = $this->ac_model->getDataByCondition(array('cate_id'=>$a_data['category_id']));
        switch ($data['bor_type']){
            case 1:$show = '<a target="_blank" href="'.url('index/BorrowsShow/read',array('id'=>$data['arc_id'],'type'=>$ac_data['cate_type'])).'">点击查看</a>';break;
            case 2:$show = '<a target="_blank" href="'.url('index/BorrowsShow/read',array('id'=>$data['arc_id'],'type'=>$ac_data['cate_type'])).'">点击查看</a>';break;
            case 3:$show = '<a target="_blank" href="'.url('index/BorrowsShow/read',array('id'=>$data['arc_id'],'type'=>$ac_data['cate_type'])).'">点击查看</a>';break;
            case 4:
                if($ac_data['cate_type']==1){
                    $url = url('index/Prearc/download',array('id'=>$data['arc_id']));
                }elseif ($ac_data['cate_type'] ==2){
                    $url = url('index/Arc/download',array('id'=>$data['arc_id']));
                }else{
                    $url = url('index/Borrows/download',array('id'=>$data['arc_id']));
                }
                $show = '<a target="_blank" href="'.$url.'">下载PDF版</a>';
            break;
            case 5:
                if($ac_data['cate_type']==1){
                    $url = url('index/Prearc/download',array('id'=>$data['arc_id']));
                }elseif ($ac_data['cate_type'] ==2){
                    $url = url('index/Arc/download',array('id'=>$data['arc_id']));
                }else{
                    $url = url('index/Borrows/download',array('id'=>$data['arc_id']));
                }
                $show = '<a target="_blank" href="'.$url.'">下载原始文件</a>';
                break;
            case 6:$show = '<a target="_blank" href="'.url('index/BorrowsShow/read',array('id'=>$data['arc_id'],'type'=>$ac_data['cate_type'])).'">点击查看</a>';break;
            case 7:$show = '<a target="_blank" href="'.url('index/BorrowsShow/read',array('id'=>$data['arc_id'],'type'=>$ac_data['cate_type'])).'">点击查看</a>';break;
            case 8:$show = '<a target="_blank" href="'.url('index/BorrowsShow/read',array('id'=>$data['arc_id'],'type'=>$ac_data['cate_type'])).'">点击查看</a>';break;
            case 9:$show = '<a target="_blank" href="'.url('index/BorrowsShow/read',array('id'=>$data['arc_id'],'type'=>$ac_data['cate_type'])).'">点击查看</a>';break;
        }
        if($data['bor_time_limit'] < date('Y-m-d')){
            $show = '<a href="javascript:;" onclick="alert(\'借阅已过期\')">已过期</a>';
        }
        echo '<h1>借阅信息</h1>';
        echo '<div><table style="width: 100%;text-align: center"><tr><th>文件名称</th><th>过期时间</th><th>操作</th></tr>';
        echo '<tr><td><a>'.$a_data['arc_name'].'.'.$a_data['arc_type'].'</a></td><td>'.$data['bor_time_limit'].'</td><td>'.$show.'</td></tr>';
        echo '</table></div>';

    }

    public function read(){
        header("Content-type: text/html; charset=utf-8");
        $id = $this->request->param('id');
        $type = $this->request->param('type');
        if($type==1){
            $url = url('index/Prearc/download',array('id'=>$id));
        }elseif ($type ==2){
            $url = url('index/Arc/download',array('id'=>$id));
        }else{
            $url = url('index/Borrows/download',array('id'=>$id));
        }
        $src = urlencode('http://'.$_SERVER['HTTP_HOST'].'/'.$url);
        $html = '<iframe src="https://view.officeapps.live.com/op/view.aspx?src='.$src.'" width="100%" height="100%" frameborder="1"></iframe>';
        echo $html;
    }


}